﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using WebGrease.Css.Extensions;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Core.Data;
using FineUIPro;

namespace CanDoo.Oil.WebApp.proProduct
{
    public partial class Default : ListPageBase<IProductContract, Product>
    {


        public IProductTypeContract ProductTypeContract { get; set; }
        public IBrandContract BrandContract { get; set; }
        public ISupplierContract SupplierContract { get; set; }
        public IProductUnitContract ProductUnitContract { get; set; }

        public override bool IsMaxMode
        {
            get
            {
                return true;
            }
        }

        #region 方法
        protected override void InitGrid()
        {
            Selector = p => new
            {
                p.SN,
                p.Code,
                p.ShortName,
                p.ProductType,
                p.ProductTypeId,
                p.Brand,
                p.BrandId,
                p.DefaultSupplier,
                p.DefaultSupplierId,
                p.ProductUnit,
                p.ProductUnitId,
                p.Specifications,
                p.Model,
                p.Weight,
                p.Unit,
                p.ImgUrl1,
                p.ImgUrl2,
                p.ImgUrl3,
                p.ImgUrl4,
                p.ImgUrl5,
                p.Year,
                p.MarketTime,
                p.Outline,
                p.Explain,
                p.TechnicalParameter,
                p.CommonProblem,
                p.Remark,
                p.MarkedPrice,
                p.DefaultCostPrice,
                p.RetailPrice,
                p.VIPPrice1,
                p.VIPPrice2,
                p.VIPPrice3,
                p.VIPPrice4,
                p.TradePrice1,
                p.TradePrice2,
                p.TradePrice3,
                p.TradePrice4,
                p.TradePrice5,
                p.TradePrice6,
                p.TradePrice7,
                p.TradePrice8,
                p.FocusTracking,
                p.IsNew,
                p.EasyStock,
                p.ProductClass,
                p.ExchangeIntegral,
                p.FrontDesk,
                p.NoIntegral,
                p.NoDiscount,
                p.Enable,
                p.ManageSerialNum,
                p.Id,
                p.Name
            };
            base.InitGrid();
        }

        protected override void Search()
        {
            var key = SearchKey.GetValue();
            var producttypeId = 0L;
            if (SearchProductType.Values.Count() > 0 && SearchProductType.Values[0].ToLong() >  0L)
                 producttypeId = SearchProductType.Values[0].ToLong();
            var brandId = 0L;
            if (SearchBrand.Values.Count() > 0 && SearchBrand.Values[0].ToLong() >  0L)
                 brandId = SearchBrand.Values[0].ToLong();
            var defaultsupplierId = 0L;
            if(string.IsNullOrEmpty(key) == false)
            {
                Filter = Filter.And(p => p.Name.Contains(key));
            }
            if (producttypeId >  0L)
                Filter = Filter.And(p => p.ProductTypeId == producttypeId);
            if (brandId >  0L)
                Filter = Filter.And(p => p.BrandId == brandId);
            if (defaultsupplierId >  0L)
                Filter = Filter.And(p => p.DefaultSupplierId == defaultsupplierId);

            Filter = Filter.And(p => p.ProductClass==0);

            var isDeleted = ManageRecycle.Checked;
            Filter = Filter.And(p => p.IsDeleted == isDeleted);
        }
        #endregion

        #region 事件
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitGrid();
            }
        }
        #endregion

        #endregion 事件

    }
}
